<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Utils\OSS;
use App\Utils\Tools;
use Illuminate\Http\Request;
use App\Models\UploadFiles;

class CommonController extends Controller
{
    public function __construct(Request $request)
    {
        parent::__construct($request);
    }

    /**
     * @return void
     */
    public function uploadFile()
    {
        $retData = [
            'url' => env('APP_FILE_URL'),
        ];
        $fileData = $this->requestData->file('file');
        if (empty($fileData)) {
            throwError("未获取到文件");
        }
        if (!empty($fileData->getError())) {
            throwError($fileData->getErrorMessage());
        }
        $file_hash = hash_file('sha256', $fileData->getRealPath());
        $uploadFile = UploadFiles::where(['file_hash' => $file_hash, 'file_size' => $fileData->getSize()])->first();
        if ($uploadFile) {
            $retData['path'] = $uploadFile->file_path;
            $retData['id'] = $uploadFile->id;
            SetReturnJson($retData, 200, '上传成功');
        }
        $fileName = Tools::getUuid();
        $res = '/' . (new OSS())->addFile($fileData, date('Ymd') . '/' . $fileName);
        $retData['path'] = $res;
        $id = UploadFiles::insertGetId([
            'channel' => 20,
            'storage' => 'oss',
            'domain' => env('APP_FILE_URL'),
            'file_type' => $fileData->getClientMimeType(),
            'file_name' => $fileName . '.' . $fileData->getClientOriginalExtension(),
            'file_path' => $res,
            'file_full_path' => env('APP_FILE_URL') . '/' . $res,
            'file_size' => $fileData->getSize(),
            'file_ext' => $fileData->getClientOriginalExtension(),
            'file_hash' => $file_hash,
            'user_id' => 0,
            'is_recycle' => 0,
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s')
        ]);
        $retData['id'] = $id;
        SetReturnJson($retData);
    }

    /**获取轮播图
     * @return void
     */
    public function getBanners()
    {
        $banners = Banners::whereNull('deleted_at')->orderBy('sort_idx', 'desc')->get(['id', 'title', 'banner_type', 'cover', 'cover_url']);
        SetReturnJson($banners);
    }

    /**获取省市区
     * @return void
     */
    public function getRegionList()
    {
        $pid = $this->requestData->get('pid') ?: 0;
        $region = Region::where(['pid' => $pid])->get();
        SetReturnJson($region);
    }

    /**意见反馈
     * @return void
     */
    public function proposalBack()
    {
        $data = $this->requestData->post();
        $user = getUser();
        $res = UserProposal::create([
            'user_id' => $user->id,
            'name' => $data['name'],
            'phone' => $data['phone'],
            'content' => $data['content'],
            'imgs' => $data['imgs'] ?: '',
        ]);
        $res && SetReturnJson([]);
        throwError("反馈失败,请稍后重试!");
    }

    /**收藏/取消收藏
     * @return void
     */
    public function doFavorite()
    {
        $user = getUser();
        $data = $this->requestData->post();
        $type = $data['type'] ?: 1;
        $tbl = $type == 1 ? 'goods' : 'pet_dossiers';
        $tbl_ids = explode(',', $data['tbl_id']);
        foreach ($tbl_ids as &$tbl_id) {
            if ($favoriteFavorite = Favorite::where(['user_id' => $user->id, 'tbl' => $tbl, 'tbl_id' => $tbl_id])->first()) {
                $favoriteFavorite->delete();
            } else {
                $favoriteFavorite = new Favorite();
                $favoriteFavorite->user_id = $user->id;
                $favoriteFavorite->tbl = $tbl;
                $favoriteFavorite->tbl_id = $data['tbl_id'];
                $favoriteFavorite->save();
            }
        }
        SetReturnJson([]);
    }

    /**查询物流
     * @return void
     */
    public function getExpress()
    {
        $express_no = $this->requestData->get('express_no') ?? '';
        if (!$express_no) {
            throwError('缺少快递单号');
        }
        $express = logistics::where(['express_no' => $express_no])->first();
        if ($express) {
            $express->data = json_decode($express->data, true);
        }
        SetReturnJson($express);
    }

    /**快递公司列表
     * @return void
     */
    public function getExpressCompany()
    {
        $list = Express::where('express_id', '<', 32)->orderBy('sort', 'desc')->orderBy('express_id', 'asc')->select(['express_id', 'express_name', 'express_code', 'express_type'])->get();
        SetReturnJson($list);
    }

    /**获取字段描述
     * @return void
     */
    public function getFieldDesc()
    {
        SetReturnJson([
            'eye' => [ //眼睛
                '正常', '流眼泪', '发炎'
            ],
            'nose' => [//鼻子
                '正常', '打喷嚏', '流鼻涕'
            ],
            'ears' => [ //耳朵
                '正常', '耳螨'
            ],
            'hair' => [ //毛发
                '正常', '猫藓', '皮肤红点'
            ],
            'butt' => [ //屁股
                '正常', '猫藓', '皮肤红点'
            ],
            'feces' => [ //粪便
                '正常', '软便', '拉稀', '便秘'
            ],
            'appetite' => [ //食量
                '正常', '一般', '胃口不好'
            ],
            'spirit' => [ //精神
                '正常', '良好', '一般', '较差', '很差'
            ],
        ]);
    }
}
